#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "studeng.h"
#include "teacher.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_timer = new QTimer(this);
    m_timer->setInterval(1000);
    m_timer->stop();

    // connect(m_timer,SIGNAL(timeout()),this,SLOT(on_timer_timeOut()));
    connect(m_timer,&QTimer::timeout,this,&MainWindow::on_timer_timeOut);

    this->tech = new Teacher();
    this->stu = new Student();

    // 这是一种写法
    // QObject::connect(tech,SIGNAL(hungry()),stu,SLOT(qingke()));
    connect(tech,SIGNAL(hungry()),stu,SLOT(qingke()));

    // 这是一种写法,带参数
    void(Teacher:: *techSig)(const QString &) = &Teacher::hungry;
    void(Student:: *stuSlot)(const QString &) = &Student::qingke;
    connect(tech,techSig,stu,stuSlot);

}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 * @brief MainWindow::on_pushButton_clicked
 * 获取当前时间
 */
void MainWindow::on_pushButton_clicked()
{
    QDateTime qdt = QDateTime::currentDateTime();

    ui->timeEdit->setTime(qdt.time());
    ui->dateEdit->setDate(qdt.date());
    ui->dateTimeEdit->setDateTime(qdt);

    ui->timeStr->setText(qdt.toString("HH:mm:ss"));
    ui->dateStr->setText(qdt.toString("yyyy-MM-dd"));
    ui->dateTimeStr->setText(qdt.toString("yyyy-MM-dd HH:mm:ss"));

}
/**
 * @brief MainWindow::on_pushButton_2_clicked
 * 设置时间
 */
void MainWindow::on_pushButton_2_clicked()
{
    QString text = ui->timeStr->text();
    QTime time = QTime::fromString(text,"HH:mm:ss");
    ui->timeEdit->setTime(time);

}

/**
 * @brief MainWindow::on_pushButton_3_clicked
 * 设置日期
 */
void MainWindow::on_pushButton_3_clicked()
{
    QString text = ui->dateStr->text();
    QDate date = QDate::fromString(text,"yyyy-MM-dd");
    ui->dateEdit->setDate(date);
}
/**
 * @brief MainWindow::on_pushButton_4_clicked
 * 设置日期时间
 */
void MainWindow::on_pushButton_4_clicked()
{
     QString text = ui->dateTimeStr->text();
     QDateTime dateTime = QDateTime::fromString(text,"yyyy-MM-dd HH:mm:ss");
     ui->dateTimeEdit->setDateTime(dateTime);
}

void MainWindow::on_timer_timeOut()
{
    QTime curTime = QTime::currentTime();
    ui->lcdNumber->display(curTime.hour());
    ui->lcdNumber_2->display(curTime.minute());
    ui->lcdNumber_3->display(curTime.second());

    ui->progressBar->setValue(curTime.second());
}

/**
 * @brief MainWindow::on_pushButton_8_clicked
 * 设置周期
 */
void MainWindow::on_pushButton_8_clicked()
{

    int value = ui->spinBox->value();
    if(value > 0){
        m_timer->setInterval(value);
    }
}

/**
 * @brief MainWindow::on_pushButton_5_clicked
 * 开始
 */
void MainWindow::on_pushButton_5_clicked()
{
    m_timer->start(1000);

    m_time->start();
}

/**
 * @brief MainWindow::on_pushButton_6_clicked
 * 停止
 */
void MainWindow::on_pushButton_6_clicked()
{
    m_timer->stop();
    int count = m_time->elapsed();
    QString str("已过去:%1,毫秒");
    str = str.arg(count);
    ui->label_6->setText(str);

}

/**
 * @brief MainWindow::on_calendarWidget_activated
 * @param date 日期
 * 选中日历中的日期的时候触发，双击。
 */
void MainWindow::on_calendarWidget_activated(const QDate &date)
{
    ui->lineEdit_4->setText(date.currentDate().toString("yyyy-MM-dd"));
}
/**
 * 下课后，触发老师饿了
 * @brief MainWindow::on_pushButton_9_clicked
 */
void MainWindow::on_pushButton_9_clicked()
{
    emit this->tech->hungry();
}

void MainWindow::on_pushButton_10_clicked()
{
    emit this->tech->hungry("小龙虾");
}

